//
//  AppDelegate.swift
//  迷你微博
//
//  Created by Json on 2020/2/2.
//  Copyright © 2020 Json. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
                
        window = UIWindow()
        window?.backgroundColor = UIColor.white
       // window?.rootViewController = WBHomeViewController()
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }
}

// 从服务器加载应用程序信息
extension AppDelegate {
    
    // 模拟异步
    private func loadAppInfo() {
        // url 模拟采用本地数据
        let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
        // data
        let data = NSData(contentsOf: url!)
        // 写入磁盘
        let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
        
        // 直接保存在沙盒中，等待下次程序使用
        data?.write(toFile: jsonPath, atomically: true)
        
        print("应用程序加载完毕 \(jsonPath)")
    }
}

